﻿using System.Collections.Generic;
using System.Linq;
using DroidClaim.DAL.Entities;
using NHibernate;
using NHibernate.Criterion;
using System;
using DroidClaim.DAL.Models;


namespace DroidClaim.DAL.Dal
{
    public class ClaimNumberDAL
    {
        private readonly ISession _session;
        public ClaimNumberDAL(ISession session)
        {
             this._session = session;
        }

        public List<ClaimNumber> GetAll(ClaimNumberSearch searchCriteria)
        {
            var criteria = _session.CreateCriteria(typeof(ClaimNumber));

            if (searchCriteria != null)
            {
                // Search	
		
									
											if (!string.IsNullOrEmpty(searchCriteria.Code))
						{
							criteria.Add(Expression.Like("Code", searchCriteria.Code, MatchMode.Start));
						}
						
															
											if (!string.IsNullOrEmpty(searchCriteria.Name))
						{
							criteria.Add(Expression.Like("Name", searchCriteria.Name, MatchMode.Start));
						}
						
										
			

                //Sort
                if (!string.IsNullOrEmpty(searchCriteria.Sort))
                {
                    if (!string.IsNullOrEmpty(searchCriteria.SortType) && searchCriteria.SortType.ToLower() == "desc")
                    {
                        criteria.AddOrder(Order.Desc(searchCriteria.Sort));
                    }
                    else
                    {
                        criteria.AddOrder(Order.Asc(searchCriteria.Sort));
                    }

                }
            }

            return criteria.List<ClaimNumber>().ToList();
        }

        public void Add(ClaimNumber claimnumber)
        {
            _session.Save( claimnumber);
        }

        public void Update(ClaimNumber claimnumber)
        {
			 claimnumber = _session.Merge(claimnumber);
            _session.Update( claimnumber);
        }

        public void Delete(List<string> idList)
        {
            int row = _session.CreateQuery("DELETE ClaimNumber where Id in (:IdList)").SetParameterList("IdList", idList).ExecuteUpdate();
            if (row != idList.Count)
                throw new Exception("Delete Not Success");

        }
    }
}

